package com.easymin.weixin.manager.impl;

import com.easymin.weixin.common.DataForm;
import com.easymin.weixin.common.DataGrid;
import com.easymin.weixin.entity.WxApplication;
import com.easymin.weixin.manager.WxApplicationManager;
import com.easymin.weixin.service.WxApplicationService;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DefaultWxApplicationManager implements WxApplicationManager {

  @Autowired
  private WxApplicationService wxApplicationService;

  @Override
  public DataGrid<WxApplication> findAll(int page, int rows) {

    int offset = (page - 1) * rows;

    RowBounds rowBounds = new RowBounds(offset, rows);

    List<WxApplication> wxApplications = wxApplicationService.findAll(rowBounds);
    long total = wxApplicationService.count();

    DataGrid<WxApplication> dataGrid = new DataGrid<WxApplication>();
    dataGrid.setRows(wxApplications);
    dataGrid.setTotal(total);

    return dataGrid;
  }

  @Override
  public List<WxApplication> findAll() {

    List<WxApplication> wxApplications = wxApplicationService.findAll();
    return wxApplications;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public DataForm<WxApplication> insert(WxApplication wxApplication) {

    DataForm<WxApplication> dataForm = new DataForm<WxApplication>();
    WxApplication application = wxApplicationService.exists(wxApplication.getName());

    if (application == null) {
      wxApplicationService.insert(wxApplication);
      dataForm.setStatus(0);
      dataForm.setMessage("成功");
      dataForm.setData(wxApplication);
    } else {
      dataForm.setStatus(-1);
      dataForm.setMessage("应用名称已经存在");
    }

    return dataForm;
  }

  @Override
  public WxApplication fetch(Long id) {

    return wxApplicationService.fetch(id);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public DataForm<WxApplication> updateByPrimaryKey(WxApplication wxApplication) {

    DataForm<WxApplication> dataForm = new DataForm<>();

    WxApplication local = wxApplicationService.fetch(wxApplication.getId());

    if (null != local) {
      WxApplication application = wxApplicationService.exists(wxApplication.getName());

      if (application == null 
          || (application != null && application.getId().equals(local.getId()))) {
        local.setName(wxApplication.getName());
        local.setAddress(wxApplication.getAddress());
        local.setWxkey(wxApplication.getWxkey());
        wxApplicationService.update(local);

        dataForm.setData(local);
        dataForm.setStatus(0);
        dataForm.setMessage("成功");
      } else {
        dataForm.setStatus(-2);
        dataForm.setMessage("微信应用名称已经存在");
      }
    } else {
      dataForm.setStatus(-1);
      dataForm.setMessage("微信应用不存在");
    }


    return dataForm;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public DataForm<WxApplication> deleteByPrimaryKey(Long id) {

    DataForm<WxApplication> dataForm = new DataForm<>();

    wxApplicationService.delete(id);
    dataForm.setStatus(0);
    dataForm.setMessage("成功");

    return dataForm;
  }

  @Override
  public WxApplication findByAppKey(String appKey) {

    return wxApplicationService.findByAppKey(appKey);
  }
  
  @Override
  public WxApplication findByAddress(String address) {
    
    return wxApplicationService.findByAddress(address);
  }
}
